/**
 * Created by wangzhili on 2017/9/25.
 */
//展示集群列表
function showClusterList() {
    var url = "/cykmr/cluster/list";
    $.ajax({
        url: url,
        dataType: 'json',
        success: function (data) {
            paging_now();
            initClusterList(data);
        }
    });
}
//初始化集群列表
function initClusterList(data) {
       $('#cluster_tbody').html("");
       getClusterList(data).forEach(function (item) {
           $('#cluster_tbody').append(item);
       })
}
//返回一个tr的集合，其中tr的内容为某一条集群信息
function getClusterList(data) {
    var clusterList = [];
    for (var i = 0; i< data.length; i++){
        clusterList.push(getClusterTr(data[i], i))
    }
    return clusterList;
}
//返回一个装有集群信息的tr
function getClusterTr(item, i) {
        var $td1 = $('<td></td>');
        $td1.attr("class","center").html(i+1);
        var $td2 = $('<td></td>');
        $td2.attr("class","center").html(item.clusterName);
        var $td3 = $('<td></td>');
        $td3.attr("class","center").html("hdfs://"+item.ip+":"+item.port);

        var $td4 = $('<td></td>');
        var transTime = new Date(item.createTime);
        $td4.attr("class","center").html(transTime.toLocaleDateString()+" "+transTime.toTimeString().substring(0,8));
        var $td5 = $('<td></td>');
        var transTime1 = new Date(item.updateTime);
        $td5.attr("class","center").html(transTime1.toLocaleDateString()+" "+transTime1.toTimeString().substring(0,8));


        var $button1 = $('<button></button>');
        $button1.attr("class", "btn btn-danger btn-small").css("margin","0 5px").html("删除");
        const id= item.clusterId;
        $button1.click(function () {
            deleteCluster(id);
        });
        var $button2 = $('<button></button>');
        $button2.attr("class", "btn btn-primary btn-small").attr("data-toggle", "modal").attr("data-target", "#editClusterModal").css("margin","0 5px").html("修改");
        $button2.click(function () {
            getCluster(id, initEditDialog);
        });

        var $td6 = $('<td></td>').attr("class","center");
        $td6.append($button1);
        $td6.append($button2);

        var $tr = $('<tr></tr>');
        $tr.attr('class', 'cluster_tr');
        $tr.append($td1).append($td2).append($td3).append($td4).append($td5).append($td6);

       return $tr;
}
//根据集群ID获取某条集群信息
function getCluster(clusterId,callback) {
    var url = "/cykmr/cluster/queryByClusterId";
    $.ajax({
        url: url,
        dataType: 'json',
        data:{
            clusterId:clusterId
        },
        success:function (data) {
            callback(data.result);
        }
    })
}
//集群信息绑定到到模态框相应位置
function initEditDialog(data) {
    $('#editClusterId').val(data.clusterId);
    $('#editClusterName').val(data.clusterName);
    $('#editClusterIp').val(data.ip);
    $('#editClusterPort').val(data.port);
    $('#editClusterUserName').val(data.userName);
    $('#editClusterPassword').val(data.userPassword);
}
//添加
function addCluster() {
    var clusterName = $('#clusterName').val();
    var clusterIp = $('#clusterIp').val();
    var port = $('#clusterPort').val();
    var userName = $('#userName').val();
    var userPassword = $('#userPassword').val();

    var url = "/cykmr/cluster/add";
    $.ajax({
        url: url,
        dataType: 'json',
        data: {
            clusterName: clusterName,
            ip: clusterIp,
            port: port,
            username: userName,
            password: userPassword
        },
        success: function (data) {
            if (data){
                alert("添加成功！");
                $('#addClusterModal').modal('toggle');

            }else{
                alert("添加失败！");
            }

        }
    });
}
//删除
function deleteCluster(clusterId) {
    bootbox.confirm("确定要删除吗?", function (result) {
        if (result) {
            var url = "/cykmr/cluster/delete";
            $.ajax({
                url: url,
                dataType: 'json',
                data: {
                    clusterId: clusterId
                },
                success: function (data) {
                    if (data){
                        alert("删除成功！");
                    }else{
                        alert("删除失败！");
                    }
                    showClusterList();
                }
            });

        }
    });
}
//修改
function editCluster() {
    var id = $('#editClusterId').val();
    var editClusterName = $('#editClusterName').val();
    var editClusterIp = $('#editClusterIp').val();
    var editClusterPort = $('#editClusterPort').val();
    var editClusterUserName = $('#editClusterUserName').val();
    var editClusterPassword = $('#editClusterPassword').val();

    var url = "/cykmr/cluster/edit";
    $.ajax({
        url: url,
        dataType: 'json',
        data: {
            clusterId: id,
            clusterName: editClusterName,
            ip: editClusterIp,
            port: editClusterPort,
            username: editClusterUserName,
            password: editClusterPassword
        },
        success: function (data) {
            if (data){
                alert("修改成功！");
                $('#editClusterModal').modal('toggle');
            }else{
                alert("修改失败！");
            }
        }
    });


}
//模态框的监听事件
$(function () {
    //关闭addClusterModal模态框触发
    $('#addClusterModal').on('hide.bs.modal',
        function () {
            showClusterList();
        });
    //关闭模态框触发
    $('#editClusterModal').on('hide.bs.modal',
    function () {
        showClusterList();
    })
})